<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Support\Arr;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        ResponseException::class,
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param  \Exception  $exception
     * @return void
     */
    public function report(Exception $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        return parent::render($request, $exception);
    }



//    protected function unauthenticateds($request, AuthenticationException $exception)
//	{
//
//		return parent::unauthenticated($request, $exception); // TODO: Change the autogenerated stub
//	}

	/**
	 * 重定义身份验证错误异常
	 * User: ihome
	 * @param \Illuminate\Http\Request $request
	 * @param AuthenticationException $exception
	 * @return \Illuminate\Http\RedirectResponse|\Symfony\Component\HttpFoundation\Response
	 */
	protected function unauthenticated($request, AuthenticationException $exception)
	{
		if ($request->expectsJson()) {
			return response()->json(['error' => 'Unauthenticated.'], 401);
		}

		if(in_array('admin', $exception->guards())) {
			return redirect()->guest(route('admin.login'));
		}

//		$arr = [
//			$exception->getMessage(),
//			$exception->guards(),
//			$exception->getCode(),
//			$exception->getTrace(),
//		];

        if($request->redirectToUrl == 'admin.login'){
            return redirect()->guest(route('admin.login'))->with('danger','登录信息已过期，请重新登录');
        }else{
            return redirect()->guest($exception->redirectTo() ?? route('login'))->with('danger','登录异常，请重新登录');
        }

        //		return $request->expectsJson()
//			? response()->json(['message' => $exception->getMessage()], 401)
//			: redirect()->guest($exception->redirectTo() ?? route('login'));
	}



	/**
	 * Convert the given exception to an array.
	 *
	 * @param  \Exception  $e
	 * @return array
	 */
	protected function convertExceptionToArray(Exception $e)
	{
		return config('app.debug') ? [
			'message' => $e->getMessage(),
			'exception' => get_class($e),
			'file' => $e->getFile(),
			'line' => $e->getLine(),
			'trace' => collect($e->getTrace())->map(function ($trace) {
				return Arr::except($trace, ['args']);
			})->all(),
		] : [
			'code'=>'500',
			'msg' => $this->isHttpException($e) ? $e->getMessage() : '服务器错误，请稍后重试',
		];
	}
}
